package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.supplier3.SupplierRecommendEntity;
import com.sgb.kjwl.model.entity.user.PromoteGoodsEntity;
import com.sgb.kjwl.view.ui.activity.manage.ShopInfoForCompanyTradesActivity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopInfoActivity;
import com.sgb.kjwl.view.ui.adapter.supplier3.PromoteGoodsAdapter;
import com.sgb.kjwl.view.ui.adapter.supplier3.SupplierRecommendAdapter;
import com.swgk.core.dialog.BaseBottomDialog;
import com.swgk.core.dialog.DialogViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

public class GoodsInfoDialogList extends BaseBottomDialog {

    private List<SupplierRecommendEntity> entity;
    private XRecyclerView goodsList;
    private RelativeLayout rlClose;
    private DialogItemClickCallBack dialogClickCallBack;
    private SupplierRecommendAdapter promoteGoodsAdapter;
    private Context context;

    public GoodsInfoDialogList(Context context, DialogCallBack dialogCallBack) {
        super(context, dialogCallBack);
    }


    public GoodsInfoDialogList(Context context, int theme, DialogCallBack dialogCallBack) {
        super(context, theme, dialogCallBack);
    }

    @Override
    protected int dialogLayout() {
        return R.layout.map_info_dialog_list;
    }

    @Override
    protected void initView(DialogViewHolder holder, Bundle savedInstanceState) {
        setCancelable(false);
        goodsList = holder.getDialog().findViewById(R.id.goods_list);
        rlClose = holder.getDialog().findViewById(R.id.rl_close);
        rlClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodsInfoDialogList.this.cancel();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        goodsList.setLayoutManager(layoutManager);
        goodsList.setPullRefreshEnabled(false);
        goodsList.setLoadingMoreEnabled(false);
        promoteGoodsAdapter = new SupplierRecommendAdapter(getContext(), entity);
        goodsList.setAdapter(promoteGoodsAdapter);
        promoteGoodsAdapter.setOnItemClick(new AdapterOnItemClick<SupplierRecommendEntity>() {
            @Override
            public void onItemClick(SupplierRecommendEntity supplierRecommendEntity, int position) {
                if (TextUtils.isEmpty(supplierRecommendEntity.getCompanyId())) {
                    ToastUtil.show(getContext(), "企业店铺id不能为空");
                    return;
                }
                ShopInfoForCompanyTradesActivity.startAct(getContext(), supplierRecommendEntity.getCompanyId(), "", 0, 0);
                GoodsInfoDialogList.this.cancel();
            }
        });
    }

    public List<SupplierRecommendEntity> getData() {
        return entity;
    }

    public GoodsInfoDialogList setData(List<SupplierRecommendEntity> entity) {
        this.entity = entity;
        promoteGoodsAdapter.addDatas(entity);
        promoteGoodsAdapter.notifyDataSetChanged();
        return this;
    }

    public interface DialogItemClickCallBack {
        void onClickShop(String shopId);
    }

    public GoodsInfoDialogList setDialogItemClickCallBack(DialogItemClickCallBack dialogClickCallBack) {
        this.dialogClickCallBack = dialogClickCallBack;
        return this;
    }
}
